package com.example.demo.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.domain.Tag;
import com.example.demo.mapper.TagMapper;

@Service
public class TagServiceImpl implements TagService {
	@Autowired
	TagMapper tagMapper;

	/****
	 * article_tag表中只有tid与aid，需要通过aid查询到tid集合， 再从tag表中以tid得到对应的Tag
	 * 
	 */
	@Override
	public List<Tag> getTagsOfArticleByAid(Long aid) {
		// 文章对应的所有tid
		List<Integer> allTagId = new ArrayList<Integer>();
		// 文章对应的所有标签
		List<Tag> tags = new ArrayList<Tag>();
		if (aid != null) {
			allTagId = tagMapper.selectAllTagIdByAid(aid);
			for (Integer tid : allTagId) {
				tags.add(tagMapper.selelctTagByTid(tid));
			}
			return tags;
		} else {
			return null;
		}
	}

	@Override
	public List<Tag> getTags() {

		return tagMapper.selectTags();
	}

	@Override
	public Integer addTagOfArticle(Long aid, Integer tid) {
		// 判空，调用
		if (aid != null && tid != null) {
			return tagMapper.insertTagOfArticle(aid, tid);
		}
		return null;
	}

	@Override
	public List<Long> getAllAidBytid(Integer tid) {
		if (tid == null) {
			return null;
		}
		return tagMapper.selectAllAidByTid(tid);
	}

}
